<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>chip.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-graphicsview-chip-example.html">40000 Chips</a></td><td >chip.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">chip.cpp 示例文件</h1>
<span class="subtitle">graphicsview/chip/chip.cpp</span>
<!-- $$$graphicsview/chip/chip.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the demonstration applications of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &quot;chip.h&quot;</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>

  Chip<span class="operator">::</span>Chip(<span class="keyword">const</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> <span class="operator">&amp;</span>color<span class="operator">,</span> <span class="type">int</span> x<span class="operator">,</span> <span class="type">int</span> y)
  {
      <span class="keyword">this</span><span class="operator">-</span><span class="operator">&gt;</span>x <span class="operator">=</span> x;
      <span class="keyword">this</span><span class="operator">-</span><span class="operator">&gt;</span>y <span class="operator">=</span> y;
      <span class="keyword">this</span><span class="operator">-</span><span class="operator">&gt;</span>color <span class="operator">=</span> color;
      setZValue((x <span class="operator">+</span> y) <span class="operator">%</span> <span class="number">2</span>);

      setFlags(ItemIsSelectable <span class="operator">|</span> ItemIsMovable);
      setAcceptHoverEvents(<span class="keyword">true</span>);
  }

  <span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span> Chip<span class="operator">::</span>boundingRect() <span class="keyword">const</span>
  {
      <span class="keyword">return</span> <span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">110</span><span class="operator">,</span> <span class="number">70</span>);
  }

  <span class="type"><a href="../qtgui/qpainterpath.html">QPainterPath</a></span> Chip<span class="operator">::</span>shape() <span class="keyword">const</span>
  {
      <span class="type"><a href="../qtgui/qpainterpath.html">QPainterPath</a></span> path;
      path<span class="operator">.</span>addRect(<span class="number">14</span><span class="operator">,</span> <span class="number">14</span><span class="operator">,</span> <span class="number">82</span><span class="operator">,</span> <span class="number">42</span>);
      <span class="keyword">return</span> path;
  }

  <span class="type">void</span> Chip<span class="operator">::</span>paint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> <span class="operator">*</span>painter<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstyleoptiongraphicsitem.html">QStyleOptionGraphicsItem</a></span> <span class="operator">*</span>option<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget)
  {
      Q_UNUSED(widget);

      <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> fillColor <span class="operator">=</span> (option<span class="operator">-</span><span class="operator">&gt;</span>state <span class="operator">&amp;</span> <span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>State_Selected) <span class="operator">?</span> color<span class="operator">.</span>dark(<span class="number">150</span>) : color;
      <span class="keyword">if</span> (option<span class="operator">-</span><span class="operator">&gt;</span>state <span class="operator">&amp;</span> <span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>State_MouseOver)
          fillColor <span class="operator">=</span> fillColor<span class="operator">.</span>light(<span class="number">125</span>);

      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> lod <span class="operator">=</span> option<span class="operator">-</span><span class="operator">&gt;</span>levelOfDetailFromTransform(painter<span class="operator">-</span><span class="operator">&gt;</span>worldTransform());
      <span class="keyword">if</span> (lod <span class="operator">&lt;</span> <span class="number">0.2</span>) {
          <span class="keyword">if</span> (lod <span class="operator">&lt;</span> <span class="number">0.125</span>) {
              painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(<span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">110</span><span class="operator">,</span> <span class="number">70</span>)<span class="operator">,</span> fillColor);
              <span class="keyword">return</span>;
          }

          <span class="type"><a href="../qtgui/qbrush.html">QBrush</a></span> b <span class="operator">=</span> painter<span class="operator">-</span><span class="operator">&gt;</span>brush();
          painter<span class="operator">-</span><span class="operator">&gt;</span>setBrush(fillColor);
          painter<span class="operator">-</span><span class="operator">&gt;</span>drawRect(<span class="number">13</span><span class="operator">,</span> <span class="number">13</span><span class="operator">,</span> <span class="number">97</span><span class="operator">,</span> <span class="number">57</span>);
          painter<span class="operator">-</span><span class="operator">&gt;</span>setBrush(b);
          <span class="keyword">return</span>;
      }

      <span class="type"><a href="../qtgui/qpen.html">QPen</a></span> oldPen <span class="operator">=</span> painter<span class="operator">-</span><span class="operator">&gt;</span>pen();
      <span class="type"><a href="../qtgui/qpen.html">QPen</a></span> pen <span class="operator">=</span> oldPen;
      <span class="type">int</span> width <span class="operator">=</span> <span class="number">0</span>;
      <span class="keyword">if</span> (option<span class="operator">-</span><span class="operator">&gt;</span>state <span class="operator">&amp;</span> <span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>State_Selected)
          width <span class="operator">+</span><span class="operator">=</span> <span class="number">2</span>;

      pen<span class="operator">.</span>setWidth(width);
      <span class="type"><a href="../qtgui/qbrush.html">QBrush</a></span> b <span class="operator">=</span> painter<span class="operator">-</span><span class="operator">&gt;</span>brush();
      painter<span class="operator">-</span><span class="operator">&gt;</span>setBrush(<span class="type"><a href="../qtgui/qbrush.html">QBrush</a></span>(fillColor<span class="operator">.</span>dark(option<span class="operator">-</span><span class="operator">&gt;</span>state <span class="operator">&amp;</span> <span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>State_Sunken <span class="operator">?</span> <span class="number">120</span> : <span class="number">100</span>)));

      painter<span class="operator">-</span><span class="operator">&gt;</span>drawRect(<span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(<span class="number">14</span><span class="operator">,</span> <span class="number">14</span><span class="operator">,</span> <span class="number">79</span><span class="operator">,</span> <span class="number">39</span>));
      painter<span class="operator">-</span><span class="operator">&gt;</span>setBrush(b);

      <span class="keyword">if</span> (lod <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">1</span>) {
          painter<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="../qtgui/qpen.html">QPen</a></span>(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>gray<span class="operator">,</span> <span class="number">1</span>));
          painter<span class="operator">-</span><span class="operator">&gt;</span>drawLine(<span class="number">15</span><span class="operator">,</span> <span class="number">54</span><span class="operator">,</span> <span class="number">94</span><span class="operator">,</span> <span class="number">54</span>);
          painter<span class="operator">-</span><span class="operator">&gt;</span>drawLine(<span class="number">94</span><span class="operator">,</span> <span class="number">53</span><span class="operator">,</span> <span class="number">94</span><span class="operator">,</span> <span class="number">15</span>);
          painter<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="../qtgui/qpen.html">QPen</a></span>(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>black<span class="operator">,</span> <span class="number">0</span>));
      }

      <span class="comment">// Draw text</span>
      <span class="keyword">if</span> (lod <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">2</span>) {
          <span class="type"><a href="../qtgui/qfont.html">QFont</a></span> font(<span class="string">&quot;Times&quot;</span><span class="operator">,</span> <span class="number">10</span>);
          font<span class="operator">.</span>setStyleStrategy(<span class="type"><a href="../qtgui/qfont.html">QFont</a></span><span class="operator">::</span>ForceOutline);
          painter<span class="operator">-</span><span class="operator">&gt;</span>setFont(font);
          painter<span class="operator">-</span><span class="operator">&gt;</span>save();
          painter<span class="operator">-</span><span class="operator">&gt;</span>scale(<span class="number">0.1</span><span class="operator">,</span> <span class="number">0.1</span>);
          painter<span class="operator">-</span><span class="operator">&gt;</span>drawText(<span class="number">170</span><span class="operator">,</span> <span class="number">180</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;Model: VSC-2000 (Very Small Chip) at %1x%2&quot;</span>)<span class="operator">.</span>arg(x)<span class="operator">.</span>arg(y));
          painter<span class="operator">-</span><span class="operator">&gt;</span>drawText(<span class="number">170</span><span class="operator">,</span> <span class="number">200</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;Serial number: DLWR-WEER-123L-ZZ33-SDSJ&quot;</span>));
          painter<span class="operator">-</span><span class="operator">&gt;</span>drawText(<span class="number">170</span><span class="operator">,</span> <span class="number">220</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;Manufacturer: Chip Manufacturer&quot;</span>));
          painter<span class="operator">-</span><span class="operator">&gt;</span>restore();
      }

      <span class="comment">// Draw lines</span>
      <span class="type"><a href="../qtcore/qvarlengtharray.html">QVarLengthArray</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span><span class="operator">,</span> <span class="number">36</span><span class="operator">&gt;</span> lines;
      <span class="keyword">if</span> (lod <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0.5</span>) {
          <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span><span class="operator">=</span> <span class="number">10</span>; i <span class="operator">+</span><span class="operator">=</span> (lod <span class="operator">&gt;</span> <span class="number">0.5</span> <span class="operator">?</span> <span class="number">1</span> : <span class="number">2</span>)) {
              lines<span class="operator">.</span>append(<span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span>(<span class="number">18</span> <span class="operator">+</span> <span class="number">7</span> <span class="operator">*</span> i<span class="operator">,</span> <span class="number">13</span><span class="operator">,</span> <span class="number">18</span> <span class="operator">+</span> <span class="number">7</span> <span class="operator">*</span> i<span class="operator">,</span> <span class="number">5</span>));
              lines<span class="operator">.</span>append(<span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span>(<span class="number">18</span> <span class="operator">+</span> <span class="number">7</span> <span class="operator">*</span> i<span class="operator">,</span> <span class="number">54</span><span class="operator">,</span> <span class="number">18</span> <span class="operator">+</span> <span class="number">7</span> <span class="operator">*</span> i<span class="operator">,</span> <span class="number">62</span>));
          }
          <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span><span class="operator">=</span> <span class="number">6</span>; i <span class="operator">+</span><span class="operator">=</span> (lod <span class="operator">&gt;</span> <span class="number">0.5</span> <span class="operator">?</span> <span class="number">1</span> : <span class="number">2</span>)) {
              lines<span class="operator">.</span>append(<span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span>(<span class="number">5</span><span class="operator">,</span> <span class="number">18</span> <span class="operator">+</span> i <span class="operator">*</span> <span class="number">5</span><span class="operator">,</span> <span class="number">13</span><span class="operator">,</span> <span class="number">18</span> <span class="operator">+</span> i <span class="operator">*</span> <span class="number">5</span>));
              lines<span class="operator">.</span>append(<span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span>(<span class="number">94</span><span class="operator">,</span> <span class="number">18</span> <span class="operator">+</span> i <span class="operator">*</span> <span class="number">5</span><span class="operator">,</span> <span class="number">102</span><span class="operator">,</span> <span class="number">18</span> <span class="operator">+</span> i <span class="operator">*</span> <span class="number">5</span>));
          }
      }
      <span class="keyword">if</span> (lod <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0.4</span>) {
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span> lineData<span class="operator">[</span><span class="operator">]</span> <span class="operator">=</span> {
              <span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span>(<span class="number">25</span><span class="operator">,</span> <span class="number">35</span><span class="operator">,</span> <span class="number">35</span><span class="operator">,</span> <span class="number">35</span>)<span class="operator">,</span>
              <span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span>(<span class="number">35</span><span class="operator">,</span> <span class="number">30</span><span class="operator">,</span> <span class="number">35</span><span class="operator">,</span> <span class="number">40</span>)<span class="operator">,</span>
              <span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span>(<span class="number">35</span><span class="operator">,</span> <span class="number">30</span><span class="operator">,</span> <span class="number">45</span><span class="operator">,</span> <span class="number">35</span>)<span class="operator">,</span>
              <span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span>(<span class="number">35</span><span class="operator">,</span> <span class="number">40</span><span class="operator">,</span> <span class="number">45</span><span class="operator">,</span> <span class="number">35</span>)<span class="operator">,</span>
              <span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span>(<span class="number">45</span><span class="operator">,</span> <span class="number">30</span><span class="operator">,</span> <span class="number">45</span><span class="operator">,</span> <span class="number">40</span>)<span class="operator">,</span>
              <span class="type"><a href="../qtcore/qlinef.html">QLineF</a></span>(<span class="number">45</span><span class="operator">,</span> <span class="number">35</span><span class="operator">,</span> <span class="number">55</span><span class="operator">,</span> <span class="number">35</span>)
          };
          lines<span class="operator">.</span>append(lineData<span class="operator">,</span> <span class="number">6</span>);
      }
      painter<span class="operator">-</span><span class="operator">&gt;</span>drawLines(lines<span class="operator">.</span>data()<span class="operator">,</span> lines<span class="operator">.</span>size());

      <span class="comment">// Draw red ink</span>
      <span class="keyword">if</span> (stuff<span class="operator">.</span>size() <span class="operator">&gt;</span> <span class="number">1</span>) {
          <span class="type"><a href="../qtgui/qpen.html">QPen</a></span> p <span class="operator">=</span> painter<span class="operator">-</span><span class="operator">&gt;</span>pen();
          painter<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="../qtgui/qpen.html">QPen</a></span>(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>red<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>SolidLine<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RoundCap<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RoundJoin));
          painter<span class="operator">-</span><span class="operator">&gt;</span>setBrush(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoBrush);
          <span class="type"><a href="../qtgui/qpainterpath.html">QPainterPath</a></span> path;
          path<span class="operator">.</span>moveTo(stuff<span class="operator">.</span>first());
          <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">1</span>; i <span class="operator">&lt;</span> stuff<span class="operator">.</span>size(); <span class="operator">+</span><span class="operator">+</span>i)
              path<span class="operator">.</span>lineTo(stuff<span class="operator">.</span>at(i));
          painter<span class="operator">-</span><span class="operator">&gt;</span>drawPath(path);
          painter<span class="operator">-</span><span class="operator">&gt;</span>setPen(p);
      }
  }

  <span class="type">void</span> Chip<span class="operator">::</span>mousePressEvent(<span class="type"><a href="qgraphicsscenemouseevent.html">QGraphicsSceneMouseEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="type"><a href="qgraphicsitem.html">QGraphicsItem</a></span><span class="operator">::</span>mousePressEvent(event);
      update();
  }

  <span class="type">void</span> Chip<span class="operator">::</span>mouseMoveEvent(<span class="type"><a href="qgraphicsscenemouseevent.html">QGraphicsSceneMouseEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>modifiers() <span class="operator">&amp;</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ShiftModifier) {
          stuff <span class="operator">&lt;</span><span class="operator">&lt;</span> event<span class="operator">-</span><span class="operator">&gt;</span>pos();
          update();
          <span class="keyword">return</span>;
      }
      <span class="type"><a href="qgraphicsitem.html">QGraphicsItem</a></span><span class="operator">::</span>mouseMoveEvent(event);
  }

  <span class="type">void</span> Chip<span class="operator">::</span>mouseReleaseEvent(<span class="type"><a href="qgraphicsscenemouseevent.html">QGraphicsSceneMouseEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="type"><a href="qgraphicsitem.html">QGraphicsItem</a></span><span class="operator">::</span>mouseReleaseEvent(event);
      update();
  }

</pre>
</div>
<!-- @@@graphicsview/chip/chip.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
